package com.mengze.linlee_iot_adnroid.ui.notifications

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.mengze.linlee_iot_adnroid.databinding.ItemMessageBinding


class MessageAdapter(
    private val items: List<Message>,
    private val onItemClick: (Message) -> Unit
): RecyclerView.Adapter<MessageAdapter.ViewHolder>() {

    class ViewHolder(val binding: ItemMessageBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bind(message: Message) {
            binding.tvTitle.text = message.title
            binding.tvDetail.text = message.detail
            binding.tvTime.text = message.createTime
            binding.ivUnread.visibility = if (message.isUnRead) View.VISIBLE else View.GONE
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        println("onCreateViewHolder~~~~~~~")
        val binding = ItemMessageBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
        return ViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        var item = items[position];
        holder.binding.tvTitle.text = item.title;
        holder.binding.tvDetail.text = item.detail;
        holder.binding.tvTime.text = item.createTime;
        holder.itemView.setOnClickListener { onItemClick(item) }
    }

    override fun getItemCount() = items.size
}


data class Message (
    val id: Long = 0,
    val title: String,
    val detail: String,
    val createTime: String,
    val isUnRead: Boolean = false,
    val messageType: Int = 0
){


}